package com.paic.mhis.hcpms.quota.exportword.utils;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;

/**  
 * 通用方法集合
 * @author:     EX-XIONGFEI002
 * @version:    
 */
public class GeneralMethods {

	private GeneralMethods() {
	}

	private static GeneralMethods instance = new GeneralMethods();

	public static GeneralMethods getInstance() {
		return instance;
	}

	/**
	 * 替换掉/ 2015/03 2015-03 
	 * @param timeStr  timeStr
	 * @return timeStr
	 */
	public String replacTimeFormate(String timeStr) {

		if (isEmptyString(timeStr)) {
			return null;
		}

		return timeStr.replace("/", "-");
	}

	/**
	 * 获取当前月份的字符串格式，如果小于10，则在前面加0    
	 * @return 当前月份
	 */
	public String getMonthDateStr() {
		Calendar calendar = Calendar.getInstance();

		return calendar.get(Calendar.MONTH) + 1 >= 10 ? "0" + calendar.get(Calendar.MONTH) + 1 : calendar
				.get(Calendar.MONTH) + 1 + "";
	}

	/**
	 * 获取当前月份    
	 * @param format   格式
	 * @param date	   date
	 * @return 格式化后时间
	 */
	public String getFormatDate(String format, Date date) {

		return new SimpleDateFormat(format).format(date);
	}

	/**
	 * 根据当前时间推算之前或之后的月份时间
	 * @return 月份数
	 */
	public Calendar addMonthByNowDate(int addMonths, String kpiTime) {

		String year = kpiTime.split("-")[0];
		String month = kpiTime.split("-")[1];

		Calendar calendar = Calendar.getInstance();

		calendar.set(Calendar.YEAR, Integer.parseInt(year));
		calendar.set(Calendar.MONTH, Integer.parseInt(month));

		calendar.add(Calendar.MONTH, addMonths);

		return calendar;
	}

	/**
	 * 根据当前时间推算之前或之后的月份时间
	 * @return 月份数
	 */
	public Date addMonthDate(int addMonths) {

		Calendar calendar = Calendar.getInstance();

		calendar.add(Calendar.MONTH, addMonths);

		return calendar.getTime();
	}

	/**
	 * 根据当前时间推算之前或之后的月份时间
	 * @return 月份数
	 */
	public Calendar addMonths(int addMonths) {

		Calendar calendar = Calendar.getInstance();

		calendar.add(Calendar.MONTH, addMonths);

		return calendar;
	}

	/**
	 * 根据当前时间推算之前或之后的月份时间
	 * @return 月份数
	 */
	public String addNMonthToFormate(String formate, int addMonths) {
		Calendar addMonthByNowDate = addMonths(addMonths);

		return getFormatDate(formate, addMonthByNowDate.getTime());
	}

	/**
	 * 根据当前时间推算之前或之后的月份时间
	 * @return 天数
	 */
	public Calendar addDayByNowDate(int addAdds) {

		Calendar calendar = Calendar.getInstance();

		calendar.add(Calendar.DAY_OF_MONTH, addAdds);

		return calendar;
	}

	/**
	 * 如果有null值，填充List    
	 * @param srcList	srcList
	 * @param length	length	
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List fullNullList(List srcList, int length) {
		List destList = new ArrayList();

		if (srcList != null) {
			destList.addAll(srcList);

			for (int i = 0; i < length - srcList.size(); i++) {
				destList.add(new BigDecimal(0));
			}
		} else {
			for (int i = 0; i < length; i++) {
				destList.add(new BigDecimal(0));
			}
		}

		return destList;
	}

	/**
	 * 判断小数
	 * @param input input
	 * @return 结果
	 */
	public boolean checkDouble(String input) {
		if (input == null) {
			return false;
		}

		return input.matches("^(-)?(([1-9]+)|([0-9]+(\\.[0-9]{1,10})?))$");
	}

	/**
	 * 是否空字符串    
	 * @param input input
	 * @return 是否空字符串
	 */
	public boolean isEmptyString(String input) {
		if (input == null) {
			return true;
		}

		return input.length() == 0;
	}

	/**
	 * 是否空字符串    
	 * @param input input
	 * @return 是否空字符串
	 */
	public boolean isNotEmptyString(String input) {
		return !isEmptyString(input);
	}

	/**
	 * 是否空字集合    
	 * @param input input
	 * @return 是否空字符串
	 */
	@SuppressWarnings("rawtypes")
	public boolean isEmptyCollects(Collection coll) {
		if (coll == null) {
			return true;
		}

		return coll.isEmpty();
	}

	/**
	 * 是否空字集合    
	 * @param input input
	 * @return 是否空字符串
	 */
	@SuppressWarnings("rawtypes")
	public boolean isNotEmptyCollects(Collection coll) {
		return !isEmptyCollects(coll);
	}

	/**
	 * 添加返回值
	 * @param paramsMap  入参
	 * @param resultMap  出参
	 * 
	 */
	public void addAllValueToMap(Map<String, Object> paramsMap, Map<String, Object> resultMap) {
		if (paramsMap == null) {
			return;
		}

		Set<Entry<String, Object>> entrySet = paramsMap.entrySet();

		for (Entry<String, Object> entry : entrySet) {
			resultMap.put(entry.getKey(), entry.getValue());
		}
	}

	/**
	 * 在中文后面加空格
	 * @param content 内容
	 * @return 内容
	 * 
	 */
	public String addBlankForString(String content) {
		if (GeneralMethods.getInstance().isEmptyString(content)) {
			return content;
		}

		StringBuffer buff = new StringBuffer();
		for (int i = 0; i < content.length(); i++) {
			if (content.charAt(i) > 255 || content.charAt(i) < 0) {
				buff.append(content.charAt(i)).append(" ");
			} else {
				buff.append(content.charAt(i));
			}
		}
		return buff.toString();

	}

	/**
	 * 将一个百分数的数字，转化成百分数的小数数值    
	 * @param num
	 * @return
	 */
	public double parsePercentage(double num) {
		if (num == 0) {
			return 0;
		}

		double nNum = num / 100;

		DecimalFormat formate = new DecimalFormat("0.0000");
		return Double.parseDouble(formate.format(nNum));
	}

	/**
	 * 保留2位小数    
	 * @param num num
	 * @return string
	 */
	public String toFix2Digits(double num) {
		DecimalFormat formate = new DecimalFormat("0.00");
		return formate.format(num);
	}

	/**
	 * 创建一个itext Phrase    
	 * @param str str
	 * @return Phrase
	 * @throws IOException 
	 * @throws DocumentException 
	 */
	public Phrase createPhraseWithNormal(String str) throws DocumentException, IOException {

		Phrase phrase = null;
		BaseFont chinese = BaseFont.createFont();
		Font contextFont = new Font(chinese, 9, Font.NORMAL);
		phrase = new Phrase(str, contextFont);

		return phrase;
	}

	/**
	 * 创建一个itext Phrase   粗体  
	 * @param str str
	 * @return Phrase			  Phrase
	 * @throws IOException        IOException
	 * @throws DocumentException  DocumentException 
	 */
	public Phrase createPhraseWithBold(String str) throws DocumentException, IOException {
		Phrase phrase = null;
		BaseFont chinese = BaseFont.createFont();
		Font contextFont = new Font(chinese, 9, Font.BOLD);
		phrase = new Phrase(str, contextFont);

		return phrase;
	}

	public String reversalStr(String str) {
		if (str.length() < 2 || str == null)
			return str;
		return reversalStr(str.substring(1)) + str.charAt(0);
	}

	public static void main(String[] args) {
		System.out.println(GeneralMethods.getInstance().reversalStr("yyyy-MM"));
	}
}
